﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls.Primitives;

namespace WpfWordsLearner.Behaviors
{
	public class SelectorCommandExtensionBehavior : Behavior<Selector>
	{
		#region _____Enums________________

		public enum ExecutionTypes
		{
			ItemClick,
			ItemDoubleClick
		}

		#endregion

		#region _____Deps_________________

		public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(SelectorCommandExtensionBehavior), null);
		public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object),
																										typeof(SelectorCommandExtensionBehavior), null);
		public static readonly DependencyProperty ExecutionTypeProperty = DependencyProperty.Register("ExecutionType", typeof(ExecutionTypes), typeof(SelectorCommandExtensionBehavior),
																										new PropertyMetadata(ExecutionTypes.ItemDoubleClick));

		#endregion

		#region _____Interface____________

		public ICommand Command
		{
			get { return (ICommand)GetValue(CommandProperty); }
			set { SetValue(CommandProperty, value); }
		}

		public object CommandParameter
		{
			get { return GetValue(CommandParameterProperty); }
			set { SetValue(CommandParameterProperty, value); }
		}

		public ExecutionTypes ExecutionType
		{
			get { return (ExecutionTypes)GetValue(ExecutionTypeProperty); }
			set { SetValue(ExecutionTypeProperty, value); }
		}

		#endregion

		#region _____Private Implementation____________

		void ExecuteCommand()
		{
			if (Command != null)
				Command.Execute(CommandParameter);
		}

		void AssociatedObject_MouseDoubleClick(object sender, MouseButtonEventArgs e)
		{
			if (GetHit(e) != null && ExecutionType == ExecutionTypes.ItemDoubleClick)
				ExecuteCommand();
		}

		void AssociatedObject_MouseUp(object sender, MouseButtonEventArgs e)
		{
			if (GetHit(e) != null && ExecutionType == ExecutionTypes.ItemClick)
				ExecuteCommand();
		}

		DependencyObject GetHit(RoutedEventArgs e)
		{
			if (e.OriginalSource == null) return null;
			var dependencyObject = e.OriginalSource as DependencyObject;
			return dependencyObject == null ? null : this.AssociatedObject.ContainerFromElement(dependencyObject);
		}

		#endregion

		#region _____Overrides____________

		protected override void OnAttached()
		{
			this.AssociatedObject.MouseDoubleClick += AssociatedObject_MouseDoubleClick;
			this.AssociatedObject.MouseUp += AssociatedObject_MouseUp;
			base.OnAttached();
		}

		protected override void OnDetaching()
		{
			this.AssociatedObject.MouseDoubleClick -= AssociatedObject_MouseDoubleClick;
			this.AssociatedObject.MouseUp -= AssociatedObject_MouseUp;
			base.OnDetaching();
		}

		#endregion
	}
}
